package wiki.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import wiki.banco.Conexao;
import wiki.beans.Estado;

public class UfDAO 
{
	Conexao conexao;
	
	
	public UfDAO()
	{
		conexao = new Conexao();
	}
	
	
	protected void finalize()
	{
		conexao.fechar();
	}
	
	
	public ArrayList<Estado> buscarTodos()
	{
	    ArrayList<Estado> arrayRegistros = new ArrayList<Estado>();
	    
	    String query = "SELECT idUF, Nome, codPais FROM uf ORDER BY idUF";
	    conexao.preparar(query);
	    
	    try
	    {
	      ResultSet rs = conexao.executeQuery();
	      
	      while (rs.next())
	      {
	    	  arrayRegistros.add(new Estado(rs.getString("idUF"), rs.getString("Nome"), rs.getInt("codPais")));
	      }
	      
	      rs.close();
	      
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(UfDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    
	    return arrayRegistros;
	}
	
	public ArrayList<String> buscarTodosNomes()
	{
	    ArrayList<String> arrayRegistros = new ArrayList<String>();
	    
	    String query = "SELECT Nome FROM uf ORDER BY idUF";
	    conexao.preparar(query);
	    
	    try
	    {
	      ResultSet rs = conexao.executeQuery();
	      
	      while (rs.next())
	      {
	    	  arrayRegistros.add(rs.getString("Nome"));
	      }
	      
	      rs.close();
	      
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(UfDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    
	    return arrayRegistros;
	}
	
	@SuppressWarnings("finally")
	public Estado buscarPorID(String id)
	{
	    Estado UF = null;
	    String query = "SELECT idUF, Nome, codPais FROM UF WHERE idUF=?";

	    conexao.preparar(query);
	    try
	    {
	      conexao.getPstmt().setString(1, id);
	      ResultSet rs = conexao.executeQuery();
	      
	      while (rs.next())
	      {
	    	  UF = new Estado(id, rs.getString("Nome"), rs.getInt("codPais"));
	      }
	      rs.close();
	      
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(UfDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    finally
	    {
	      return UF;
	    }
	}
	
	
	@SuppressWarnings("finally")
	public Estado buscarPorNome(String nome)
	{
	    Estado UF = null;
	    String query = "SELECT idUF, Nome, codPais FROM UF WHERE Nome=?";

	    conexao.preparar(query);
	    try
	    {
	      conexao.getPstmt().setString(1, nome);
	      ResultSet rs = conexao.executeQuery();
	      
	      while (rs.next())
	      {
	    	  UF = new Estado(rs.getString("idUF"), rs.getString("nome"), rs.getInt("codPais"));
	      }
	      rs.close();
	      
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(UfDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    finally
	    {
	      return UF;
	    }
	}

}
